package mwa.cs.rq.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mwa.cs.dao.AddressDAO;
import mwa.cs.dao.CustomerDAO;
import mwa.cs.dao.DefaultAddressDAO;
import mwa.cs.dao.GenDocDAO;
import mwa.cs.dao.MeterSizeDAO;
import mwa.cs.dao.ReceiveCodeDAO;
import mwa.cs.dao.RequestDAO;
import mwa.cs.dao.RequestQuoteDAO;
import mwa.cs.dao.RequestTypeDAO;
import mwa.cs.dao.SMErrorLogDAO;
import mwa.cs.dao.VatRateDAO;
import mwa.cs.database.Database;
import mwa.cs.model.Customer;
import mwa.cs.model.DefaultAddress;
import mwa.cs.model.Request;
import mwa.cs.model.RequestQuote;
import mwa.cs.util.JDate;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class CS62000Srvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int vnSiteId;
	private int vnUserId;
	private String vaAgentBrowser;
	private String vaUrlAddress;
	boolean vbUsedJSon = false;
	private String requestNumber;
       
    public CS62000Srvl() {
        super();
    }

	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {	
//		HttpSession session = request.getSession();
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
		vnUserId = Integer.parseInt(request.getSession().getAttribute("user_id").toString());
	
		vaUrlAddress = request.getParameter("url_address");
		vaAgentBrowser = request.getParameter("agent_browser");
		String vaProvince = request.getParameter("province");
		String vaDistrict = request.getParameter("district");
		String vaCity = request.getParameter("city");
		String vaRequestType = request.getParameter("requestType");
		String vaDefaultAddress = request.getParameter("vaDefaultAddress");
		
		String selectedCalculateByAccount = request.getParameter("selectedCalculateByAccount");
		String selectedCalculateByRequest = request.getParameter("selectedCalculateByRequest");
		String vaBtnSave = request.getParameter("btn_save");
		String statusButton = request.getParameter("statusButton");
		String vaBtn_search_account = request.getParameter("btn_search_account");
		String vaBtn_search_requestNumber = request.getParameter("btn_search_requestNumber");
		DecimalFormat voDecimalFormat = new DecimalFormat("###,##0.00");
		
		String vaStatusInsert = "no", vaNotExistStatus = "no";
		String[] vaMessage = null, vaValue = null, vaReceiveCOde = null, vaDataFromObject = null;
		Database db = new Database();
		try {
			if ( vaDefaultAddress != null && vaDefaultAddress.equals("yes") ) {
				DefaultAddressDAO voDefaultAddressDAO = new DefaultAddressDAO(db);
				DefaultAddress voDefaultAddress = voDefaultAddressDAO.getDefaultAddressBySiteId(vnSiteId);
				vaDataFromObject = new String[4];
				vaDataFromObject[0] = voDefaultAddress.getVaProvinceCode();
				vaDataFromObject[1] = voDefaultAddress.getVaDistrictCode();
				vaDataFromObject[2] = voDefaultAddress.getVaCityCode();
				vaDataFromObject[3] = voDefaultAddress.getVaPostCode();		
			} else if (vaProvince != null && vaProvince.equals("yes")) {
				AddressDAO voAddress = new AddressDAO(db);
				List<Map<String, Object>> voResult = voAddress.findProvince();
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("PROVINCE"); 
					vaMessage[i] = (String) voObject.get("NAME");		
				}
			} else if (vaDistrict != null && vaDistrict.equals("yes")) {
				AddressDAO voAddress = new AddressDAO(db);
				List<Map<String, Object>> voResult = voAddress.findDistrict(vaProvince);
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("DISTRICT"); 
					vaMessage[i] = (String) voObject.get("NAME");		
				}			
			} else if (vaCity != null && vaCity.equals("yes")) {
				AddressDAO voAddress = new AddressDAO(db);
				List<Map<String, Object>> voResult = voAddress.findCity(vaProvince, vaDistrict);
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("CITY"); 
					vaMessage[i] = (String) voObject.get("NAME");
				}	
			} else if (vaRequestType != null && vaRequestType.equals("yes")) {
				RequestTypeDAO voRequestType = new RequestTypeDAO(db);
				List<Map<String, Object>> voResult = voRequestType.findRequestTypeByRequestType(vnSiteId);
				vaValue = new String[voResult.size()];
				vaMessage = new String[voResult.size()];
				for (int i = 0; i < voResult.size(); i++) {
					Map<String, Object> voObject = voResult.get(i);	
					vaValue[i] = (String) voObject.get("REQUEST_TYPE"); 
					vaMessage[i] = (String) voObject.get("REQUEST_DESCRIPTION");
				}	
			} else if ( selectedCalculateByAccount != null && selectedCalculateByAccount.equals("vaCalculate")){
				String vaAccount = request.getParameter("inputAccountPassJSon");
				String vaDwlRequestType = request.getParameter("dwlRequestType");
				CustomerDAO voCustomerDAO = new CustomerDAO(db);
				MeterSizeDAO voMeterSizeDAO = new MeterSizeDAO(db);
				Customer voCustomer =  voCustomerDAO.findCustomerByAccount(vnSiteId, vaAccount);
				String vaMeterSize = voCustomer.getVaMeterSize();
				List<Map<String, Object>> voMeterSizeLis =  voMeterSizeDAO.findDescriptionInReceiveCodeByRequestTypeAndMeterSize(vnSiteId, vaDwlRequestType, vaMeterSize);
				vaReceiveCOde = new String[voMeterSizeLis.size()];
				vaValue = new String[voMeterSizeLis.size()];
				vaMessage = new String[voMeterSizeLis.size()];
				for ( int i = 0; i < voMeterSizeLis.size(); i++){
					Map<String, Object> voObject = voMeterSizeLis.get(i);
					vaReceiveCOde[i] =  voObject.get("RECEIVE_CODE").toString();
					vaMessage[i] =  voObject.get("DESCRIPTION").toString();
					vaValue[i] = voDecimalFormat.format(Double.parseDouble(((BigDecimal) voObject.get("FEE")).toString()));
				}
	    	} else if ( selectedCalculateByRequest != null && selectedCalculateByRequest.equals("vaCalculate") ){
	    		String vaAccount = request.getParameter("inputAccountPassJSon");
				String vaRequestNumber = request.getParameter("inputRequestNumberPassJSon");
				RequestQuoteDAO voRequestQuoteDAO = new RequestQuoteDAO(db);
				RequestDAO voRequestDAO = new RequestDAO(db);
				CustomerDAO voCustomerDAO = new CustomerDAO(db);
				MeterSizeDAO voMeterSizeDAO = new MeterSizeDAO(db);
				Customer voCustomer =  voCustomerDAO.findCustomerByAccount(vnSiteId, vaAccount);
				String vaMeterSize = voCustomer.getVaMeterSize();
				String vaDwlRequestType = request.getParameter("dwlRequestType");
				String vaRequestTypeFromRequest = voRequestDAO.getRequestTypeByRequestNumber(vnSiteId, vaRequestNumber);
				List<Map<String, Object>> voMeterSizeLis =  voMeterSizeDAO.findDescriptionInReceiveCodeByRequestTypeAndMeterSize(vnSiteId, vaDwlRequestType, vaMeterSize);
				vaReceiveCOde = new String[voMeterSizeLis.size()];
				vaValue = new String[voMeterSizeLis.size()];
				vaMessage = new String[voMeterSizeLis.size()];
				if ( vaDwlRequestType.equals(vaRequestTypeFromRequest) ){
					for ( int i = 0; i < voMeterSizeLis.size(); i++){
						Map<String, Object> voObject = voMeterSizeLis.get(i);
						String vaReceiveCodeFromMeter = voObject.get("RECEIVE_CODE").toString();	
						String vaReceiveAmount = voRequestQuoteDAO.getReceiveCodeByRequest(vnSiteId, vaRequestNumber,vaReceiveCodeFromMeter);
						vaReceiveCOde[i] =  voObject.get("RECEIVE_CODE").toString();	
						vaMessage[i] =  voObject.get("DESCRIPTION").toString();
						vaValue[i] = voDecimalFormat.format(Double.parseDouble(vaReceiveAmount));
					}	
				} else {
					for ( int i = 0; i < voMeterSizeLis.size(); i++){
						Map<String, Object> voObject = voMeterSizeLis.get(i);
						vaReceiveCOde[i] =  voObject.get("RECEIVE_CODE").toString();
						vaMessage[i] =  voObject.get("DESCRIPTION").toString();
						vaValue[i] = voDecimalFormat.format(Double.parseDouble(((BigDecimal) voObject.get("FEE")).toString()));
					}
				}
	    	} else if (vaBtn_search_account != null && vaBtn_search_account.equals("yes")){
				String vaAccount = request.getParameter("inputAccountPassJSon");
				CustomerDAO voCustomerDAO = new CustomerDAO(db);
				AddressDAO voAddressDAO = new AddressDAO(db);
				Customer voCustomer = voCustomerDAO.findCustomerByAccount(vnSiteId, vaAccount);
				if ( voCustomer != null ){
					vaValue = new String[9];
					vaValue[0] = (voCustomer.getVaPName() == null)?(" "):(voCustomer.getVaPName());
					vaValue[1] = (voCustomer.getVaFName() == null)?(" "):(voCustomer.getVaFName());
					vaValue[2] = (voCustomer.getVaLName() == null)?(" "):(voCustomer.getVaLName());
					vaValue[3] = (voCustomer.getVaAddress() == null)?(" "):(voCustomer.getVaAddress());
					vaValue[4] = (voCustomer.getVaMeterSize() == null)?(" "):(voCustomer.getVaMeterSize());
					vaValue[5] = voCustomer.getVaProvinceCode()+":"+voAddressDAO.getProvinceName(voCustomer.getVaProvinceCode());
					vaValue[6] = voCustomer.getVaDistrictCode()+":"+voAddressDAO.getDistrictName(voCustomer.getVaProvinceCode(), voCustomer.getVaDistrictCode());
					vaValue[7] = voCustomer.getVaCityCode()+":"+voAddressDAO.getCityName(voCustomer.getVaProvinceCode(), voCustomer.getVaDistrictCode(),voCustomer.getVaCityCode());
					vaValue[8] = voCustomer.getVaPostCode();
				} else {
					vaNotExistStatus = "yes";
				}
	    	} else if(vaBtnSave != null && vaBtnSave.equals("yes") && statusButton.equals("search_by_account")) {
	    		//System.out.println("welcome to save process");
	    		String vaInputFeeFromScreen = request.getParameter("vaInputFeeAndAmount");
				String vaInputFeeTemp[] = vaInputFeeFromScreen.split(",");	
				try {
					RequestDAO voRequestDAO = new RequestDAO(db);
					GenDocDAO voGenDocDAO = new GenDocDAO(db);
					ReceiveCodeDAO voReceiveCodeDAO = new ReceiveCodeDAO(db);
					RequestQuoteDAO voRequestQuoteDAO = new RequestQuoteDAO(db);
					VatRateDAO voVatRateDAO = new VatRateDAO(db);
					Request voRequest = new Request();
					requestNumber = voGenDocDAO.generatingNextRQDoc(vnSiteId, JDate.getMonth(), JDate.getYear());
					voRequest.setVnSiteId(vnSiteId);
					voRequest.setVaRequestNumber(requestNumber);
					voRequest.setVaRequestGroup("0");
					voRequest.setVaPName( request.getParameter("vaPName"));
					voRequest.setVaFName( request.getParameter("vaFName"));
					voRequest.setVaLName( request.getParameter("vaLName"));
					voRequest.setVaAddress( request.getParameter("vaAddress"));
					voRequest.setVaCityCode( request.getParameter("vaSubDistrict2"));
					voRequest.setVaDistrictCode( request.getParameter("vaDistrict2"));
					voRequest.setVaProvinceCode( request.getParameter("vaProvince2"));
					voRequest.setVaPostCode( request.getParameter("vaPostCode2"));				
					voRequest.setVaMeterSize( request.getParameter("vaMeterSize"));
					voRequest.setVaRequestType( request.getParameter("dwlRequestType"));
					voRequest.setVaApplicantName( request.getParameter("vaApplicantName"));
					voRequest.setVaDescription(request.getParameter("vaDescription"));
					voRequest.setVaAccount(request.getParameter("vaAccount"));
					voRequest.setVaContactHomeNumber(request.getParameter("vaContactHomeNumber"));
					voRequest.setVaContactAddress(request.getParameter("vaVillage"));
					voRequest.setVaContactCityCode(request.getParameter("vaSubDistrict"));
					voRequest.setVaContactDistrictCode(request.getParameter("vaDistrict"));
					voRequest.setVaContactProvinceCode(request.getParameter("vaProvince"));
					voRequest.setVaContactPostCode(request.getParameter("vaPostCode"));
					voRequest.setVaContactTelephone(request.getParameter("vaContactTelephone"));
					voRequest.setVaStatus("1");
					voRequest.setVnInsertUserId(vnUserId);
					voRequest.setVaInsertDate(JDate.getYear()+JDate.getMonth()+JDate.getDay());
					voRequest.setVaInsertTime(JDate.getHour()+JDate.getMinutes()+JDate.getSeconds());
					
					db.beginTransaction();
					// insert Request 
					voRequestDAO.insertRequest(voRequest);
	
					// check is calculate
					boolean vbIsCalculate = true;
					try {
						String vaTemp [] = vaInputFeeTemp[0].split("/");
						String vaParseReceiveCode = vaTemp[0];
						double vnParseFeeAmount = Double.parseDouble(vaTemp[1]);
					} catch (Exception e) {
						vbIsCalculate = false;
					}
					
					if ( vbIsCalculate == true ){
						for ( int i = 0; i < vaInputFeeTemp.length; i++){
							String vaTemp [] = vaInputFeeTemp[i].split("/");
							String vaReceiveCodeTemp = vaTemp[0];
							double vnFeeAmountTemp = Double.parseDouble(vaTemp[1]);
							if ( vnFeeAmountTemp != 0.0){
								RequestQuote voRequestQuote = new RequestQuote();
								voRequestQuote.setVnSiteId(vnSiteId);
								voRequestQuote.setVaRequestNumber(voRequest.getVaRequestNumber());
								voRequestQuote.setVaReceiveCode(vaReceiveCodeTemp);
								voRequestQuote.setVnReceiveAmount(vnFeeAmountTemp);
								String vaHave_Vat = voReceiveCodeDAO.findHaveVatBySiteAndReceiveCode(vnSiteId, vaReceiveCodeTemp);
								if (vaHave_Vat != null){
								Map<String, Object> voObject =	voVatRateDAO.getVatRate(vnSiteId, JDate.getYear()+JDate.getMonth()+JDate.getDay());
								String vaRate = voObject.get("RATE").toString();
									if(vaRate != null){
										voRequestQuote.setVnVatAmount(Double.parseDouble(vaRate));
									} else{
										voRequestQuote.setVnVatAmount(0);
									}
								} else {
									voRequestQuote.setVnVatAmount(0);
								}		
								voRequestQuote.setVnTotalAmount(vnFeeAmountTemp+voRequestQuote.getVnVatAmount());
								voRequestQuote.setVnInsertUserId(vnUserId);
								voRequestQuote.setVaInsertDate(voRequest.getVaInsertDate());
								voRequestQuote.setVaInsertTime(voRequest.getVaInsertTime());
								 //insert Request Quote
								voRequestQuoteDAO.insertRequestQuote(voRequestQuote);
							}
						}
					}
		    		db.commit();
		    		vaStatusInsert ="yes";	
				} catch (Exception e) {
					SMErrorLogDAO.getInstance().addLog(0, "CS62002", JDate.getDate(), JDate.getTime(),vnUserId, "CS62002Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);
		    		db.rollback();
		    	}
	    	} else if ( vaBtn_search_requestNumber != null && vaBtn_search_requestNumber.equals("yes")){
	    		String vaRequestNumber = request.getParameter("inputRequestNumberPassJSon");
	    		String vaAccount = request.getParameter("inputAccountPassJSon");
				RequestDAO voRequestDAO = new RequestDAO(db);
				RequestQuoteDAO voRequestQuoteDAO = new RequestQuoteDAO(db);
				AddressDAO voAddressDAO = new AddressDAO(db);
				CustomerDAO voCustomerDAO = new CustomerDAO(db);
				Request voRequest = voRequestDAO.findRequestByRequestNumber(vnSiteId, vaRequestNumber);
	    		if ( voRequest != null){
	    			vaDataFromObject = new String[16];
	    			vaDataFromObject[0] = voRequest.getVaAccount();
	    			vaDataFromObject[1] = voRequest.getVaPName();
	    			vaDataFromObject[2] = voRequest.getVaFName();
	    			vaDataFromObject[3] = voRequest.getVaLName();
					if ( vaAccount == null || vaAccount.equals("")){
						vaAccount =  voRequest.getVaAccount();
					}
					Customer voCustomer = voCustomerDAO.findCustomerByAccount(vnSiteId, vaAccount);
	    			vaDataFromObject[4] = voRequest.getVaAddress()+" "+voAddressDAO.getCityName(voCustomer.getVaProvinceCode(), voCustomer.getVaDistrictCode(),voCustomer.getVaCityCode())
	    					+" "+voAddressDAO.getDistrictName(voCustomer.getVaProvinceCode(), voCustomer.getVaDistrictCode())+" "+voAddressDAO.getProvinceName(voCustomer.getVaProvinceCode())
	    					+" "+voCustomer.getVaPostCode();    			
	    			vaDataFromObject[5] = voRequest.getVaMeterSize();
	    			vaDataFromObject[6] = voRequest.getVaApplicantName();
	    			vaDataFromObject[7] = voRequest.getVaContactHomeNumber();
	    			vaDataFromObject[8] = voRequest.getVaContactAddress();
	    			vaDataFromObject[9] = voRequest.getVaContactCityCode();
	    			vaDataFromObject[10] = voRequest.getVaContactDistrictCode();
	    			vaDataFromObject[11] = voRequest.getVaContactProvinceCode();
	    			vaDataFromObject[12] = voRequest.getVaContactPostCode();
	    			vaDataFromObject[13] = voRequest.getVaContactTelephone();
	    			vaDataFromObject[14] = voRequest.getVaRequestType();
	    			vaDataFromObject[15] = voRequest.getVaDescription();			
					MeterSizeDAO voMeterSizeDAO = new MeterSizeDAO(db);
					List<Map<String, Object>> voMeterSizeLis =  voMeterSizeDAO.findDescriptionInReceiveCodeByRequestTypeAndMeterSize(vnSiteId, voRequest.getVaRequestType(), voRequest.getVaMeterSize());
					vaReceiveCOde = new String[voMeterSizeLis.size()];
					vaValue = new String[voMeterSizeLis.size()];
					vaMessage = new String[voMeterSizeLis.size()];
					for ( int i = 0; i < voMeterSizeLis.size(); i++){
						Map<String, Object> voObject = voMeterSizeLis.get(i);
						String vaReceiveCodeFromMeter = voObject.get("RECEIVE_CODE").toString();	
						String vaReceiveAmount = voRequestQuoteDAO.getReceiveCodeByRequest(vnSiteId, vaRequestNumber,vaReceiveCodeFromMeter);	
						vaReceiveCOde[i] =  voObject.get("RECEIVE_CODE").toString();	
						vaMessage[i] =  voObject.get("DESCRIPTION").toString();
						vaValue[i] = voDecimalFormat.format(Double.parseDouble(vaReceiveAmount));
					}		    				    				    			
	    		} else {
	    			vaNotExistStatus = "yes";
	    		}
	    		
	    	} else if (vaBtnSave != null && vaBtnSave.equals("yes") && statusButton.equals("search_by_request")) {
	    		//System.out.println("welcome to update process");
	    		String vaInputFeeFromScreen = request.getParameter("vaInputFeeAndAmount");
	    		String vaRequestNumber = request.getParameter("vaRequestNumber");
				String vaInputFeeTemp[] = vaInputFeeFromScreen.split(",");	
				try {
					RequestDAO voRequestDAO = new RequestDAO(db);
					GenDocDAO voGenDocDAO = new GenDocDAO(db);
					ReceiveCodeDAO voReceiveCodeDAO = new ReceiveCodeDAO(db);
					RequestQuoteDAO voRequestQuoteDAO = new RequestQuoteDAO(db);
					VatRateDAO voVatRateDAO = new VatRateDAO(db);
					Request voRequest = new Request();
					voRequest.setVnSiteId(vnSiteId);
					voRequest.setVaRequestNumber( voGenDocDAO.generatingNextRQDoc(vnSiteId, JDate.getMonth(), JDate.getYear()));
					voRequest.setVaRequestType( request.getParameter("dwlRequestType"));
					voRequest.setVaApplicantName( request.getParameter("vaApplicantName"));
					voRequest.setVaDescription(request.getParameter("vaDescription"));
					voRequest.setVaContactHomeNumber(request.getParameter("vaContactHomeNumber"));
					voRequest.setVaContactAddress(request.getParameter("vaVillage"));
					voRequest.setVaContactCityCode(request.getParameter("vaSubDistrict"));
					voRequest.setVaContactDistrictCode(request.getParameter("vaDistrict"));
					voRequest.setVaContactProvinceCode(request.getParameter("vaProvince"));
					voRequest.setVaContactPostCode(request.getParameter("vaPostCode"));
					voRequest.setVaContactTelephone(request.getParameter("vaContactTelephone"));
					voRequest.setVnUpdateUserId(vnUserId);
					voRequest.setVaUpdateDate(JDate.getYear()+JDate.getMonth()+JDate.getDay());
					voRequest.setVaUpdateTime(JDate.getHour()+JDate.getMinutes()+JDate.getSeconds());
					
					db.beginTransaction();
					// Update Request 
					voRequestDAO.updateRequestByReplaceOldRequestNumber(voRequest, vaRequestNumber);
					voRequestQuoteDAO.deleteRequestQuoteByRequestNumber(vaRequestNumber);
					
					// check is calculate
					boolean vbIsCalculate = true;
					try {
						String vaTemp [] = vaInputFeeTemp[0].split("/");
						String vaParseReceiveCode = vaTemp[0];
						double vnParseFeeAmount = Double.parseDouble(vaTemp[1]);
					} catch (Exception e) {
						vbIsCalculate = false;
					}
					
					if ( vbIsCalculate == true ){
						for ( int i = 0; i < vaInputFeeTemp.length; i++){
							String vaTemp [] = vaInputFeeTemp[i].split("/");
							String vaReceiveCodeTemp = vaTemp[0];
							String  vaFeeAmount = vaTemp[1].replaceAll(",", "");
							 double vnFeeAmountTemp = Double.parseDouble(vaFeeAmount);
							if ( vnFeeAmountTemp != 0.0){
								RequestQuote voRequestQuote = new RequestQuote();
								voRequestQuote.setVnSiteId(vnSiteId);
								voRequestQuote.setVaRequestNumber(voRequest.getVaRequestNumber());
								voRequestQuote.setVaReceiveCode(vaReceiveCodeTemp);
								voRequestQuote.setVnReceiveAmount(vnFeeAmountTemp);
								String vaHave_Vat = voReceiveCodeDAO.findHaveVatBySiteAndReceiveCode(vnSiteId, vaReceiveCodeTemp);
								if (vaHave_Vat != null){
								Map<String, Object> voObject =	voVatRateDAO.getVatRate(vnSiteId, JDate.getYear()+JDate.getMonth()+JDate.getDay());
								String vaRate = voObject.get("RATE").toString();
									if(vaRate != null){
										voRequestQuote.setVnVatAmount(Double.parseDouble(vaRate));
									} else{
										voRequestQuote.setVnVatAmount(0);
									}
								} else {
									voRequestQuote.setVnVatAmount(0);
								}		
								voRequestQuote.setVnTotalAmount(vnFeeAmountTemp+voRequestQuote.getVnVatAmount());
								voRequestQuote.setVnUpdateUserId(vnUserId);
								voRequestQuote.setVaUpdateDate(voRequest.getVaUpdateDate());
								voRequestQuote.setVaUpdateTime(voRequest.getVaUpdateTime());
								 //Update Request Quote
								voRequestQuoteDAO.insertRequestQuote(voRequestQuote);
							}
						}		
					}
		    		db.commit();
		    		vaStatusInsert ="yes";	
				} catch (Exception e) {
					SMErrorLogDAO.getInstance().addLog(0, "CS62002", JDate.getDate(), JDate.getTime(),vnUserId, "CS62002Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);
		    		db.rollback();
		    	}
	    	}
			
		} finally {
			vbUsedJSon = true;
			db.close();
		}
		
		PrintWriter voOut = response.getWriter();
		try {
			JSONObject voObject = new JSONObject();
			JSONArray voList = new JSONArray();
			if (vaMessage != null) {
				for (int i = 0; i < vaMessage.length; i ++) {
					voList.add(vaMessage[i]);
				}
			}
			voObject.put("messages", voList);
			JSONArray voList2 = new JSONArray();
			if (vaValue != null) {
				for (int i = 0; i < vaValue.length; i ++) {
					voList2.add(vaValue[i]);
				}
			}
			voObject.put("values", voList2);
			
			JSONArray voList3 = new JSONArray();
			if (vaReceiveCOde != null) {
				for (int i = 0; i < vaReceiveCOde.length; i ++) {
					voList3.add(vaReceiveCOde[i]);
				}
			}
			voObject.put("receiveCOde", voList3);
			
			JSONArray voList4 = new JSONArray();
			if (vaDataFromObject != null) {
				for (int i = 0; i < vaDataFromObject.length; i ++) {
					voList4.add(vaDataFromObject[i]);
				}
			}
			voObject.put("vaDataFromObject", voList4);

			voObject.put("RequestNumber", requestNumber);
			voObject.put("vaStatusInsert",vaStatusInsert );
			voObject.put("vaNotExistStatus", vaNotExistStatus);
			voOut.print(voObject.toJSONString());
		} finally {
			voOut.close();
		}	
    	// Direct to JSP Page 
    	if ( vbUsedJSon == false){
    		RequestDispatcher voRequestDispatcher = request.getRequestDispatcher("CS62000.jsp");
    		voRequestDispatcher.forward(request, response); 
    	}
	}	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
}
